package com.example.wei.mvp.ui.presenter;

import com.example.wei.mvp.api.Api;
import com.example.wei.mvp.api.RxSchedulers;
import com.example.wei.mvp.base.RxPresenter;
import com.example.wei.mvp.contract.HelpContract;
import com.example.wei.mvp.ui.bean.PersonResult;
import com.example.wei.mvp.ui.bean.TestBean;
import javax.inject.Inject;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/5/4.
 */

public class HelpPresenter extends RxPresenter<HelpContract.View> implements HelpContract.Presenter<HelpContract.View> {


    private Api api;

    @Inject
    public HelpPresenter(Api api) {
        this.api = api;
    }

    @Override
    public void getUserStatus() {
       api.getUser()
               .compose(RxSchedulers.<TestBean>io_main())
               .compose(mView.<TestBean>bindToLife())
               .subscribe(new Consumer<TestBean>() {
                   @Override
                   public void accept(TestBean bean) throws Exception {
                       if (mView != null) {
                           mView.getUserStatus(bean);
                       }
                   }
               }, new Consumer<Throwable>() {
                   @Override
                   public void accept(Throwable throwable) throws Exception {
                       mView.showError(throwable.getMessage());
                   }
               });


    }


    @Override
    public void getUserInfo(String name,int age,String gender) {
        api.getUserInfo(name,age,gender)
                .compose(RxSchedulers.<PersonResult>io_main())
                .compose(mView.<PersonResult>bindToLife())
                .subscribe(new Consumer<PersonResult>() {
                    @Override
                    public void accept(PersonResult person) throws Exception {
                        if (mView != null) {
                            mView.getUserInfo(person);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showError(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getUserPost(String name, int age, String gender) {
        api.getUserPost(name,age,gender)
                .compose(RxSchedulers.<PersonResult>io_main())
                .subscribe(new Consumer<PersonResult>() {
                    @Override
                    public void accept(PersonResult person) throws Exception {
                        if (mView != null) {
                            mView.getUserPost(person);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showError(throwable.getMessage());
                    }
                });
    }

}
